﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Eldn.GettingStarted.EfDemos
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var ctx = new DemosContext())
            {
                Console.WriteLine("Enter a blog name:");
                var name = Console.ReadLine();

                var blog = new Blog { Name = name };
                ctx.Blogs.Add(blog);
                ctx.SaveChanges();

                var blogs = from b in ctx.Blogs orderby name select b;
                foreach (var b in blogs)
                {
                    Console.WriteLine("Blog[id: {0}, name: {1}]", b.Id, b.Name);
                }
            }

            Console.ReadKey();
        }
    }

    public class Blog
    {
        public int Id { get; set; }

        public string Name { get; set; }

        public string Url { get; set; }

        public virtual List<Post> Posts { get; set; } 
    }

    public class Post
    {
        public int Id { get; set; }

        public string Title { get; set; }

        public string Content { get; set; }

        public int BlogId { get; set; }

        public virtual Blog Blog { get; set; }
    }

    public class User
    {
        public string Name { get; set; }

        public string DisplayName { get; set; }
    }

    public class Course
    {
        public int Id { get; set; }

        public string Title { get; set; }

        public int Credits { get; set; }

        public int DepartmentId { get; set; }

        public virtual Department Department { get; set; }
    }

    public partial class OnlineCourse : Course
    {
        public string Url { get; set; }
    }

    public partial class OnsiteCourse: Course
    {
        public string Location { get; set; }

        public string Days { get; set; }

        public DateTime Time { get; set; }
    }

    public class Department
    {
        public int Id { get; set; }

        public string Name { get; set; }

        public virtual List<Course> Courses { get; set; } 
    }

    public class DemosContext : DbContext
    {
        public DbSet<Blog> Blogs { get; set; }

        public DbSet<Post> Poste { get; set; }

        public DbSet<User> Users { get; set; }

        public DbSet<Department> Departments { get; set; } 

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<User>().HasKey(u => u.Name);

            //modelBuilder.Properties().Where(p => p.Name.Contains("key")).Configure(p => p.IsKey());
            //modelBuilder.Types().Configure(c => c.ToTable(c.ClrType.Name));
        }
    }
}
